package artibella.ui.scrolls 
{
	import artibella.IDisposable;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class ScrollingDevice extends Sprite implements IDisposable
	{
		protected var _w:int;
		protected var _h:int;
		
		protected var _box:ScrollBox;
		protected var _scrollbar:ScrollBar;
		
		protected var _w_scrollbar:int = 5;
		protected var _h_scroller:int = 10;
		
		protected var _smoothMove:Boolean;
		protected var _adaptScrollSize:Boolean;
		protected var _scrollPosition:String;
		protected var _hasArrows:Boolean;
		protected var _color:int;
		
		public function ScrollingDevice( w:int, h:int, parameters:ScrollingDeviceParameters ) 
		{
			_w = w;
			_h = h;
			
			_color = parameters.color;
			_scrollPosition = parameters.scrollPosition;
			_hasArrows = parameters.hasArrows;
			_smoothMove = parameters.smoothMove;
			_adaptScrollSize = parameters.adaptScrollSize;
			
			_init();
		}
		
		protected function _init():void
		{
			_box = new ScrollBox( _w, _h );
			
			_scrollbar = new ScrollBar( _box, _smoothMove );
			
			if( _hasArrows ){
				_scrollbar.setUpBtn( _drawUpArrow() );
				_scrollbar.setDownBtn( _drawUpArrow() );
			}
			_scrollbar.setBar( _drawBar() );
			_scrollbar.setScroller( _drawScroller(), _adaptScrollSize );

			_box.setScrollingTool( _scrollbar );
			
			_box.x = ( _scrollPosition == ScrollingDeviceParameters.RIGHT ) ? 0 : 15;
			_box.y = 0;
			addChild( _box );
			
			// special
			//_box.cacheAsBitmap = true;
			//
			//var msk:Sprite = new Sprite();
			//msk.graphics.beginFill( 0xFF0000 );
			//msk.graphics.drawRect( 0, -100, _w, _h + 70 );
			//msk.graphics.endFill();
			//msk.filters = [ new BlurFilter( 0, 40, 2 ) ];
			//addChild( msk );
			//_box.mask = msk;
			//
			
			_scrollbar.x = ( _scrollPosition == ScrollingDeviceParameters.RIGHT ) ? _w + 10 : 0;
			_scrollbar.y = 0;
			addChild( _scrollbar );
		}
		
		// - - -
		
		public function setBoxContent( s:Sprite ):void
		{	
			_box.setContent( s );
		}

		// - - -
		
		protected function _drawUpArrow():Sprite
		{
			var arrow:Sprite = _drawArrow();
			arrow.x = _w_scrollbar/2 + 1;
			arrow.y = -10;
			return arrow;
		}
		
		protected function _drawDownArrow():Sprite
		{
			var arrow:Sprite = _drawArrow();
			arrow.scaleY = - 1; 
			arrow.x = _w_scrollbar/2 + 1;
			arrow.y = _h + 10;
			return arrow;
		}
		
		protected function _drawArrow():Sprite
		{
			var arrow:Sprite = new Sprite();
			arrow.graphics.beginFill( _color, 0 );
			arrow.graphics.drawRect( -_w_scrollbar / 2, -_w_scrollbar / 2, _w_scrollbar / 2, _w_scrollbar / 2 );
			arrow.graphics.endFill();
			arrow.graphics.lineStyle( 1, _color ); 
			arrow.graphics.moveTo( -3, 2 );
			arrow.graphics.lineTo( 0, -2 );
			arrow.graphics.lineTo( 3, 2 );
			return arrow;
		}
		
		protected function _drawBar():Sprite
		{
			var bar:Sprite = new Sprite();
			bar.graphics.beginFill( _color, 0 );
			bar.graphics.drawRect( 0, 0, _w_scrollbar, _h );
			bar.graphics.endFill();
			bar.graphics.lineStyle( 1, _color, .3 ); 
			bar.graphics.moveTo( _w_scrollbar/2, 0 );
			bar.graphics.lineTo( _w_scrollbar/2, _h );
			return bar;
		}
		
		protected function _drawScroller():AdaptableScroller
		{
			var scroller:AdaptableScroller = new AdaptableScroller();
			scroller.graphics.beginFill( _color );
			scroller.graphics.drawRect( 0, 0, _w_scrollbar, _h_scroller );
			scroller.graphics.endFill();
			return scroller;
		}
		
		// - - -
		
		public function get box():ScrollBox
		{
			return _box;
		}
		
		public function get scrollbar():ScrollBar
		{
			return _scrollbar;
		}
		
		// ----- IDisposable
		
		public function dispose():void
		{
			_scrollbar.dispose();
			_box.dispose();
			
			_scrollbar = null;
			_box = null;
			
		}
		
	}

}